const Mock = require('mockjs')

const roleData = Mock.mock({
  'roles|10': [{
    'id|+1': 1,
    roleName: '@cname',
    roleKey: '@word(6)',
    'status|1': [0, 1],
    createTime: '@datetime',
    remark: '@cparagraph(1)'
  }]
})

module.exports = [
  // 获取角色列表
  {
    url: '/api/system/role/list',
    method: 'get',
    response: () => {
      return {
        code: 200,
        msg: '操作成功',
        data: roleData.roles
      }
    }
  },
  
  // 根据ID获取角色信息
  {
    url: RegExp('/api/system/role/.*'),
    method: 'get',
    response: ({ url }) => {
      const roleId = url.split('/').pop()
      const role = roleData.roles.find(r => r.id == roleId)
      if (role) {
        return {
          code: 200,
          msg: '操作成功',
          data: role
        }
      } else {
        return {
          code: 404,
          msg: '角色不存在'
        }
      }
    }
  },
  
  // 新增角色
  {
    url: '/api/system/role/add',
    method: 'post',
    response: ({ body }) => {
      const newRole = {
        id: roleData.roles.length + 1,
        roleName: body.roleName,
        roleKey: body.roleKey,
        status: body.status,
        createTime: new Date().toLocaleString('zh-CN'),
        remark: body.remark
      }
      roleData.roles.push(newRole)
      return {
        code: 200,
        msg: '操作成功',
        data: null
      }
    }
  },
  
  // 更新角色
  {
    url: '/api/system/role/update',
    method: 'put',
    response: ({ body }) => {
      const index = roleData.roles.findIndex(r => r.id == body.id)
      if (index !== -1) {
        roleData.roles[index] = { ...roleData.roles[index], ...body }
        return {
          code: 200,
          msg: '操作成功',
          data: null
        }
      } else {
        return {
          code: 404,
          msg: '角色不存在'
        }
      }
    }
  },
  
  // 删除角色
  {
    url: RegExp('/api/system/role/delete/.*'),
    method: 'delete',
    response: ({ url }) => {
      const roleId = url.split('/').pop()
      const index = roleData.roles.findIndex(r => r.id == roleId)
      if (index !== -1) {
        roleData.roles.splice(index, 1)
        return {
          code: 200,
          msg: '操作成功',
          data: null
        }
      } else {
        return {
          code: 404,
          msg: '角色不存在'
        }
      }
    }
  },
  
  // 为角色分配权限
  {
    url: RegExp('/api/system/role/assignPermissions/.*'),
    method: 'post',
    response: () => {
      return {
        code: 200,
        msg: '操作成功',
        data: null
      }
    }
  },
  
  // 根据用户ID获取用户角色列表
  {
    url: RegExp('/api/system/role/user/.*'),
    method: 'get',
    response: () => {
      return {
        code: 200,
        msg: '操作成功',
        data: []
      }
    }
  }
]